package com.bgs.eat.shortMessage.service.impl;

import com.bgs.eat.config.CommonResult;
import com.bgs.eat.shortMessage.service.ShortMessageService;
import com.bgs.eat.userProtal.entity.UserProtalEntity;
import com.bgs.eat.userProtal.mapper.UserProtalMapper;
import com.bgs.eat.utils.HttpUtils;
import com.bgs.eat.utils.RedisUtil;
import com.bgs.eat.utils.ShortMessageUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ShortMessageServiceImpl
 * @Description TODO
 * @Author hbx
 * @Date 2022/10/11 20:27
 **/
@Service
public class ShortMessageServiceImpl implements ShortMessageService {

    @Autowired
    UserProtalMapper userProtalMapper;

    @Autowired
    RedisUtil redisUtil;
    /**
    * @Author hbx
    * @Description
    * @Date 2022/10/11 20:34
    * @param phone
    */
    @Override
    public CommonResult SendVerificationByPhone(String phone,String codeMark) {

            String host = "http://smsyun01.market.alicloudapi.com";
            String path = "/sms/sms02";
            String method = "POST";
            String appcode = "f97fc486f7204fffa22acb4938c4a48d";
            Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            Map<String, String> querys = new HashMap<String, String>();
            querys.put("content", "【云信】您的验证码是：147258");
            querys.put("mobile", phone);
            String bodys = "";
            try {
                HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
                System.out.println(response.toString());
                //获取response的body
                System.out.println(EntityUtils.toString(response.getEntity()));
                redisUtil.set(phone + codeMark, "147258", 300);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return CommonResult.ok();
    }
}
